package net.wanho.personalOffice.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.personalOffice.dao.DailyRecordDAO;
import net.wanho.personalOffice.po.DailyRecord;

import java.util.List;

public class DailyRecordDAOImpl extends BaseDAOImpl<DailyRecord> implements DailyRecordDAO {
    @Override
    public boolean insert(DailyRecord entity) throws DAOException {
        try {
            String sql ="INSERT INTO daily_record\n" +
                    "            (CREATOR,\n" +
                    "             TITLE,\n" +
                    "             CONTENT,\n" +
                    "             CREATE_TIME,\n" +
                    "             RECORDLX,\n" +
                    "             UPDATE_TIME)\n" +
                    "VALUES (?,?,?,?,?,?);";
            Object[] params = {
                    entity.getCreator().getEmployeeId(),
                    entity.getTitle(),
                    entity.getContent(),
                    entity.getCreateTime(),
                    entity.getRecordlx(),
                    entity.getUpdateTime()
            };
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public DailyRecord findById(String id) throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  d.RECORD_ID \"recordId\",\n" +
                    "  d.TITLE \"title\",\n" +
                    "  d.CONTENT \"content\",\n" +
                    "  d.CREATE_TIME \"createTime\",\n" +
                    "  d.RECORDLX \"recordlx\",\n" +
                    "  d.UPDATE_TIME \"updateTime\",\n" +
                    "  e.EMPLOYEE_ID \"creator.employeeId\",\n" +
                    "  e.EMPLOYEE_NAME \"creator.employeeName\",\n" +
                    "  e.DEPARTMENT_ID \"creator.departmentId\",\n" +
                    "  e.POSITON_ID \"creator.positionId\",\n" +
                    "  e.PHONE \"creator.phone\",\n" +
                    "  e.EMAIL \"creator.email\",\n" +
                    "  e.STATUS \"creator.status\",\n" +
                    "  e.PARENT_EMPLOYEE_ID \"creator.parentEmployeeId\",\n" +
                    "  e.CREATE_TIME \"creator.createTime\",\n" +
                    "  e.UPDATE_TIME \"creator.updateTime\"\n" +
                    "FROM daily_record d\n" +
                    "LEFT JOIN employee e\n" +
                    "ON d.CREATOR=e.EMPLOYEE_ID\n" +
                    "WHERE d.RECORD_ID=?";
            Object[] params = {id};
            return super.queryForObject(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<DailyRecord> selectAll() throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  d.RECORD_ID \"recordId\",\n" +
                    "  d.TITLE \"title\",\n" +
                    "  d.CONTENT \"content\",\n" +
                    "  d.CREATE_TIME \"createTime\",\n" +
                    "  d.RECORDLX \"recordlx\",\n" +
                    "  d.UPDATE_TIME \"updateTime\",\n" +
                    "  e.EMPLOYEE_ID \"creator.employeeId\",\n" +
                    "  e.EMPLOYEE_NAME \"creator.employeeName\",\n" +
                    "  e.DEPARTMENT_ID \"creator.departmentId\",\n" +
                    "  e.POSITON_ID \"creator.positionId\",\n" +
                    "  e.PHONE \"creator.phone\",\n" +
                    "  e.EMAIL \"creator.email\",\n" +
                    "  e.STATUS \"creator.status\",\n" +
                    "  e.PARENT_EMPLOYEE_ID \"creator.parentEmployeeId\",\n" +
                    "  e.CREATE_TIME \"creator.createTime\",\n" +
                    "  e.UPDATE_TIME \"creator.updateTime\"\n" +
                    "FROM daily_record d\n" +
                    "LEFT JOIN employee e\n" +
                    "ON d.CREATOR=e.EMPLOYEE_ID;";
            Object[] params = {};
            return super.queryForList(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
